package org.example.MQ.rabbitmq.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.example.MQ.rabbitmq.config.ConnectionUtil;

/**
 * @Author : zhangbo
 * @data : 2024/4/11
 * @description :生产者
 */

public class Publisher {
    private final static String QUEUE_NAME = "q_test_01";


    public void send() throws Exception {
        //1、获取连接
        Connection connection = ConnectionUtil.getConnection();
        //2、声明信道
        Channel channel = connection.createChannel();
        //3、声明(创建)队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //4、定义消息内容
        String message = "------hello rabbitmq------ ";
        //5、发布消息
        channel.basicPublish("",QUEUE_NAME,null,message.getBytes());
        System.out.println("发送消息"+message);
        //6、关闭通道
        channel.close();
        //7、关闭连接
        connection.close();
    }
}
